package club.sanmubird.springbootrabbitmq.rabbitmq.demo;

import club.sanmubird.springbootrabbitmq.rabbitmq.config.DemoMqConfig;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;


/**
 * @author Sam
 */
@Slf4j
@Component
public class DemoConsumer {

	@RabbitListener(
			bindings = {@QueueBinding(
					exchange = @Exchange(value = DemoMqConfig.EXCHANGE, type = ExchangeTypes.TOPIC),
					value = @Queue(value = DemoMqConfig.AGGREGATION_TAG_CLIENT_QUEUE_NAME, durable = "true"),
					key = {DemoMqConfig.RECEIVER_ROUTING_KEY}
			)},
			containerFactory = "demoSimpleListenerContainerFactory")

	@RabbitHandler
	public void onMessage(Message message, Channel channel) throws Exception {
		log.info("--------------------------------------");
		log.info("消费端Payload: " + message.getPayload());
		Long deliveryTag = (Long) message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);
//		//手工ACK
		channel.basicAck(deliveryTag, false);
//		channel.basicNack(deliveryTag, false, true);
//		channel.basicReject(deliveryTag, false);
	}


//	public void process(@Payload String json,
//	                    Channel channel,
//	                    @Headers Map<String, Object> headers) throws IOException {
//		log.info("--------------------------------------");
//		log.info("消费端order: " + json);
//		Long deliveryTag = (Long) headers.get(AmqpHeaders.DELIVERY_TAG);
//		//手工ACK
//		channel.basicAck(deliveryTag, false);
//	}

}
